import '../../../../../new_paltform/define/hy_header.dart';
import 'tap_item.dart';

class PopupMenuUtil {
  static Future showPopupMenu(BuildContext context,
      LongPressStartDetails details, BoxConstraints p1, List<TapItem> items) {
    final List<PopupMenuItem> popupMenuItems = <PopupMenuItem>[];

    for (TapItem item in items) {
      PopupMenuItem popupMenuItem = PopupMenuItem(
        padding: const EdgeInsets.all(1),
        child: Builder(
          builder: (BuildContext context0) {
            return GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                if (item.onTap != null) {
                  Get.back();
                  item.onTap!();
                }
              },
              child: Container(
                padding: const EdgeInsets.all(16),
                child: Row(
                  // mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Image.asset(item.img, width: 18, height: 18),
                    const SizedBox(
                      width: 10,
                    ),
                    Text(
                      item.text,
                      style: const TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      );
      popupMenuItems.add(popupMenuItem);
    }

    RenderBox? renderBox =
        Overlay.of(context)?.context.findRenderObject() as RenderBox;

    //表示位置（在花蜜啊边缘会自动调整位置）
    final RelativeRect position = RelativeRect.fromRect(
      Rect.fromLTRB(
        details.globalPosition.dx - p1.maxWidth / 1.5,
        details.globalPosition.dy + (p1.maxHeight - details.localPosition.dy),
        details.globalPosition.dx + p1.maxWidth / 1.5,
        details.globalPosition.dy,
      ),
      Offset.zero & renderBox.size,
    );

    return showMenu(
      context: context,
      position: position,
      items: popupMenuItems,
      useRootNavigator: true,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(6),
      ),
    );
  }
}
